package com.adong.web;

import com.adong.pojo.Book;
import com.adong.pojo.Cart;
import com.adong.pojo.CartItem;
import com.adong.pojo.User;
import com.adong.service.BookService;
import com.adong.service.impl.BookServiceImpl;
import com.google.gson.Gson;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author adong
 */
public class CartServlet extends BaseServlet {
    BookService bookService = new BookServiceImpl();

    /**
     * 添加到购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Deprecated
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        //获取添加图书的id
        int id = Integer.parseInt(req.getParameter("id"));
        //根据图书id获取图书信息
        Book book = bookService.queryBookById(id);
        //把图书对象信息放到
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //获取Session中的Cart对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //判断是否为null
        if (cart == null) {
            //创建Cart对象,添加到Session域中
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        //将添加的CartItem商品对象添加到cart中去
        cart.addItem(cartItem);
        req.getSession().setAttribute("lastName", cartItem.getName());
        //重定向在商品所在的地址
        resp.sendRedirect(req.getHeader("Referer"));

    }


    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取添加图书的id
        int id = Integer.parseInt(req.getParameter("id"));
        //根据图书id获取图书信息
        Book book = bookService.queryBookById(id);
        //把图书对象信息放到
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //获取Session中的Cart对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //判断是否为null
        if (cart == null) {
            //创建Cart对象,添加到Session域中
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        //将添加的CartItem商品对象添加到cart中去
        cart.addItem(cartItem);
        req.getSession().setAttribute("lastName", cartItem.getName());
        //返回商品数量和最后一个添加的商品名称
        Map<String,Object> resultMap = new HashMap<String,Object>();

        resultMap.put("totalCount", cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());

        Gson gson = new Gson();
        String resultMapJsonString = gson.toJson(resultMap);

        resp.getWriter().write(resultMapJsonString);

    }

    /**
     * 删除购物车中的商品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
        }
        //重定向在商品所在的地址
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }

    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        Integer count = Integer.parseInt(req.getParameter("count"));
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateCount(id, count);
        }
        resp.sendRedirect(req.getHeader("Referer"));
        System.out.println(req.getHeader("Referer"));
    }

}
